package com.wswy.wzcx.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.che.libcommon.ui.BaseFragment;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.model.UserMode;
import com.wswy.wzcx.ui.activity.MineActivity;
import com.wswy.wzcx.ui.other.AndroidUtilities;
import com.wswy.wzcx.ui.other.DialogFactory;
import com.wswy.wzcx.ui.other.RecycleItemClickListener;
import com.wswy.wzcx.ui.adapter.UserInfoAdapter;
import com.wswy.wzcx.ui.data.UserInfoMode;
import com.wswy.wzcx.ui.persenter.UserInfoPresenter;
import com.wswy.wzcx.ui.view.UserInfoView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangduheng26 on 2018/1/23.
 * wangduheng26@gmail.com
 */

public class UserInfoFragment extends ILoginFragment implements UserInfoView{
    public static final String TAG = "UserInfoFragment";
    private UserInfoPresenter presenter;
    public static UserInfoFragment newInstance() {

        Bundle args = new Bundle();

        UserInfoFragment fragment = new UserInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void setupPage() {
        UserMode userMode = DataCenter.get().getUserMode();
        list.clear();
        if (userMode != null) {
            list.add(new UserInfoMode("头像", userMode.headImg));
            list.add(new UserInfoMode("昵称", userMode.nickName));
            list.add(new UserInfoMode("绑定帐号", true));
            list.add(new UserInfoMode("手机", userMode.mobile));
            if (userMode.wechatBind){
                list.add(new UserInfoMode("微信", "已绑定"));
            }else {
                list.add(new UserInfoMode("微信", "未绑定"));
            }

            recyclerView.getAdapter().notifyDataSetChanged();

        }else{
            finish();
        }
    }

    RecyclerView recyclerView;
    List<UserInfoMode> list;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        presenter=new UserInfoPresenter(this);
        presenter.onCreate(getArguments());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if(presenter!=null)presenter.onDestroy();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final Context context = getContext();
        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.setPadding(0,AndroidUtilities.dp(10),0,0);
        //frameLayout.setBackgroundColor(ContextCompat.getColor(context,R.color.default_background_color));
        recyclerView = new RecyclerView(context);
        frameLayout.addView(recyclerView);
        return frameLayout;
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Context context = getContext();
        list = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setHasFixedSize(true);
        assert context != null;
        DividerItemDecoration decoration = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
        decoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.list_divider_h));
        recyclerView.addItemDecoration(decoration);
        recyclerView.addOnItemTouchListener(new RecycleItemClickListener(context, new RecycleItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                final Object tag = view.getTag();
                if(tag instanceof UserInfoMode){
                    handleClick((UserInfoMode) tag);
                }
            }
        }));
        recyclerView.setAdapter(new UserInfoAdapter(list));
    }

    private void handleClick(UserInfoMode mode) {
        if(mode!=null&&!mode.isTitle){
            if(TextUtils.equals("头像",mode.name)){
                presenter.showChooseBottomDialog();
            }else if(TextUtils.equals("昵称",mode.name)){
                startActivity(MineActivity.asIntent(getContext(),ChangeUserInfoFragment.TAG));
            }else if (TextUtils.equals("微信",mode.name)){

                if (DataCenter.get().getUserMode().wechatBind) {

                    DialogFactory
                        .create(activity(), "解除绑定", "确定要解除账号与微信的关联吗？", R.string.ok, R.string.cancel,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //actionLogout();
                                    presenter.unbindWechat();
                                }
                            }).show();
                }else {

                    //绑定
                    presenter.starWechatBind();
                }
            }
        }
    }

    @Override
    public String getFragmentTitle() {
        return "账户信息";
    }


    @Override
    public Activity activity() {
        return getActivity();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        presenter.onActivityResult(requestCode,resultCode,data);


    }

    @Override
    public BaseFragment fragment() {
        return this;
    }

    @Override
    public void dataChanged() {
        setupPage();
    }
}
